import pandas as pd
import pymysql
import threading
import time


global data1
data1 = []

def query_database(query, args=()):
    conn = pymysql.connect(host="localhost", user="root",
                           passwd="shangyi", db="jd")
    with conn.cursor() as cursor:
        cursor.execute(query, args)
        result = cursor.fetchall()
        headers = [i[0] for i in cursor.description]
        conn.commit()
    conn.close()

    data = [headers] + list(result)
    df = pd.DataFrame(data[1:], columns=data[0])
    global data1
    data1.append(df)
    return df

# 多线程查询数据库
query1 = "select * from 厨卫电器数据"
query2 = "select * from 厨卫评论数据"
query3 = "select * from 商品地址数据"

def query():
    keywords = [query1,query2,query3]
    n = len(keywords)

    t1 = threading.Thread(target=query_database, args=(keywords[0],))
    t2 = threading.Thread(target=query_database, args=(keywords[1],))
    t3 = threading.Thread(target=query_database, args=(keywords[2],))

    t1.start()
    t1.join()

    t2.start()
    t2.join()

    t3.start()
    t3.join()

    print("主函数结束")
    return data1

# T1 = time.time()
# ans = query()
# T2 = time.time()
# print(T2-T1)


